package com.base.myproject.client.ui;



import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.business.BaseRatifyDialogX;
import com.base.myproject.client.busi.business.RatifyInfo;
import com.base.myproject.client.busi.print.PrintEvn;
import com.base.myproject.client.busi.print.PrintInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Logout;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class BaseJDBCPanel extends BasePanel implements RequestCallback,Toolbar{

	
	String tablename = null; //主表名字
	String sql = null;
//	java.util.LinkedList<ToolItem> toolbarll = null;// new
//													// java.util.LinkedList<ToolItem>();
//TextToolItem 
	protected Button sava = null;
	protected Button ttiratify = null;
	protected Button print = null;
	protected Button fresh = null;
	protected java.util.LinkedList<Component>  toolbarll = new java.util.LinkedList<Component>();
	
	public String busi =null;//业务逻辑号
	protected String flowname = null;//审批流程
	java.util.LinkedList<PrintEvn> printevn = new java.util.LinkedList<PrintEvn>(); //打印格式
	public BaseJDBCPanel() {

		super();
	
		
	}

	public Component getMaincomponent() {
		return maincomponent;
	}

	public void setMaincomponent(Component maincomponent) {
		this.maincomponent = maincomponent;
	}

	protected void init() {
		super.init();
	
	
	}

	public ContentPanel initWeiget() {
		return null;
	}

	public java.util.LinkedList<Component> getToolbarll() {
		return toolbarll;
	}

	public void setToolbarll(java.util.LinkedList<Component> toolbarll) {
		this.toolbarll = toolbarll;
	}

	public String getTablename() {
		return tablename;
	}

	public void setTablename(String tablename) {

		this.tablename = tablename;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public  java.util.LinkedList<BaseStore<BaseObject>> getBasestorell() {
		return basestorell;
	}

	public void setBasestorell( java.util.LinkedList<BaseStore<BaseObject>> ll) {
		this.basestorell = ll;
	}

	public void registerStore(BaseStore bs) {
		if (basestorell == null) {

			basestorell = new   java.util.LinkedList<BaseStore<BaseObject>>();
		}
		if (!basestorell.contains(bs))
			basestorell.add(bs);

	}

	public boolean removeStore(BaseStore bs) {
		return basestorell.remove(bs);
	}

	public void removeAllStore() {
		basestorell.clear();
	}

	public int getRegisterSize() {
		return basestorell.size();
	}

	public void registerMainStore(BaseStore basestore) {
		this.mainstore = basestore;

	}

	protected void save()
	{
		
		mainstore.trySave();
	}
	public void initToolBarItems() {

		// BaseToolBar.getInstance().getItems().clear();

		sava = new Button("保存");
		sava.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.save()));
		
	//	sava.setIconStyle("disk_black");
		// tti.setIconStyle(".my-icon {background-image: url(public/baseimage/converticon.com__DB_Save16.PNG) no-repeat center left !important; }");
	//sava.setIconStyle("my-saveicon");
		sava.addSelectionListener(new SelectionListener() {
			public void componentSelected(ComponentEvent ce) {
				save();
			}
		});
		
		ttiratify = new Button("审批");
		print = new Button("打印");
		
		ttiratify.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.auction_hammer()));
		print.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		fresh =  new Button("刷新");
		//fresh.setIconStyle("basefresh");
		fresh.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.fresh()));
		
		//toolbarll.add(ttiratify);
		// initToolBar();

		Button logout = new Button("退出系统");
	
		logout.setIconStyle("logout");
		logout.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
			
				Logout logout = new Logout();
				logout.logout();
			}
			
		});
		
	/*	print.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				
				final PrintInfo pi = new PrintInfo();
				
				
				RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,URL.encode("BaseServlet"));  
				builder.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
				if(busi==null)
				{
					MessageBox.alert("警告", "业务编码没有定义!", null);  
					//return;
				}
				String form = "Action=getprinter&busino="+busi;  
				//builder.setRequestData(form);
				try {
					builder.sendRequest(form,new RequestCallback(){

						@Override
						public void onError(Request request, Throwable exception) {
							
							MessageBox.alert("错误", "获取打印的时候出错!busino="+busi, null);  
						}

						@Override
						public void onResponseReceived(Request request,
								Response response) {
							// TODO Auto-generated method stub
						//	System.out.println("success3  "+response.getText());
							
							JSONObject jsonObject =JSONParser.parse(response.getText()).isObject();
							
						
							JSONArray jsonName = jsonObject.get("name").isArray();
							JSONArray jsonValue = jsonObject.get("value").isArray();
							for(int i = 0; i < jsonName.size(); i++) {
							
							    pi.getJshm().put(jsonName.get(i).isString().stringValue(),jsonValue.get(i).isString().stringValue());
							}
							pi.setBasestorell(basestorell);
							pi.setName("zgwpi");
							
							
							for(int i=0;i<basestorell.size();i++)
							{
								
								pi.getBasetorehm().put(basestorell.get(i).getName(), basestorell.get(i));
							}
							
							
							 final PrintDialog simple = new PrintDialog(pi);  
							   simple.show();  
						}}
					);
				} catch (RequestException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				

			}
		});*/
		ttiratify.addSelectionListener(new SelectionListener() {

			@Override
			public void componentSelected(ComponentEvent ce) {
				
				ratify();
			}

		});
		fresh.addSelectionListener(new SelectionListener() {

			@Override
			public void componentSelected(ComponentEvent ce) {
				mainstore.reload();
			}
		});
		toolbarll.add(sava);
		//暂时屏蔽
		//toolbarll.add(ttiratify);
		//toolbarll.add(print);
		toolbarll.add(fresh);
		toolbarll.add(logout);
		initToolBar();
		
	}
	protected void ratify()
	{
	
		if(mainstore.getValue("icode")==null)
		{
			MessageBox.alert("错误", "不能进行提交。icode数据为空。", null);
			return;
		}
		RatifyInfo ratifyinfo = new RatifyInfo();
		ratifyinfo.setBusi(busi);
		ratifyinfo.setIcode(mainstore.getValue("icode")+"");
		ratifyinfo.setFlowname(flowname);
		BaseRatifyDialogX bdx = new BaseRatifyDialogX(ratifyinfo);
		bdx.show();
/*
//		if (mainstore != null) {
//			System.out.println("--" + mainstore.getValue("busicode"));
//		}
		Object bcode = getMainstore().getValue("bcode");
		if(bcode ==null)
		{
			MessageBox.alert("错误", "部门人员未找到，请选择正确的行", null);
		}
		BaseRatifyDialog brd = new BaseRatifyDialog(busi,Env.getEnvbcode(),bcode+"");
		//获取业务状态，如果获取不到则是10状态。
		if(mainstore.getValue("busistatus")!=null)
		brd.setBusistatus(mainstore.getValue("busistatus")+"");
		else
		{
			brd.setBusistatus("10");
		}
		if(mainstore.getValue("icode")!=null)
			brd.setIcode(mainstore.getValue("icode")+"");
			else
			{
				MessageBox.alert("错误", "为获取单据内码icode，退出", null);
			}
		BaseGrid bg = ((BaseGrid)maincomponent);
		brd.setMaintablename(bg.getTablename());
		brd.setBusistatus(bg.getValue("busistatus")+"");
		brd.show();
		*/
		
		
	/* 暂时屏蔽 执行过程TODO	if(maincomponent!=null)
		{
			if(maincomponent instanceof BaseGrid)
			{
				System.out.println(((BaseGrid)maincomponent).getValue("busicode"));
				BaseGrid bg = ((BaseGrid)maincomponent);
			
				try {
					
					sendServlet("ratify","BaseServlet", "Action=Ratify&busicode="+bg.getValue("busicode")+"&bcode="+bg.getValue("bcode")+"&busistatus="+bg.getValue("busistatus")+"&maintablename="+bg.getTablename());
				} catch (RequestException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}*/
	
	}

	public void initToolBar() {
		BaseToolBar.getInstance().removeAll();
		BaseToolBar.getInstance().add(new SeparatorToolItem());
		// BaseToolBar.getInstance().getItems().clear();
		// BaseToolBar.getInstance().add(new TextToolItem("save") );
		// BaseToolBar.getInstance().add(new TextToolItem("save1") );
		for (int i = 0; i < toolbarll.size(); i++) {
			
			BaseToolBar.getInstance().add(toolbarll.get(i));
		}
		BaseToolBar.getInstance().add(new SeparatorToolItem());
	}
	public boolean sendServlet(String servletflag,String url,String form) throws RequestException
	{
		
//		String url = "TestServlet"  ;
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,URL.encode(url));  
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
// form = "Action=aaa&age=13";  
		//builder.setRequestData(form);
		builder.sendRequest(form,this);  
		return true;
	}
	
	/**
	 * 全部初始化结束后要调用。
	 */
	public void complete()
	{
		initToolBarItems();
	}

	/**
	 * 业务编码
	 * @return
	 */
	public String getBusino() {
		return busi;
	}
/**
 * 设置业务编码
 * @param busino
 */
	public void setBusino(String busino) {
		this.busi = busino;
		
	}

	@Override
	public void onError(Request request, Throwable exception) {
		// TODO Auto-generated method stub
		System.out.println("error...");
		
	}

	@Override
	public void onResponseReceived(Request request, Response response) {
		// TODO Auto-generated method stub
		System.out.println("success  "+response.getText());
		
	}
}
